package edu.ccut.saturn.component.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class XMLHelper {

	public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

	public static final String TAG_START = "<";

	public static final String TAG_END = ">";

	public static String getElementStartTag(String elementName) {

		if (notEmptyStr(elementName)) {
			return (new StringBuffer()).append(TAG_START).append(elementName)
					.append(TAG_END).toString();
		}
		return null;
	}

	public static String getElementTailTag(String elementName) {

		if (notEmptyStr(elementName)) {
			return (new StringBuffer()).append("</").append(elementName)
					.append(TAG_END).toString();
		}
		return null;
	}

	public static String getElementStartTagWithAttributes(String elementName,
			Map<String, String> attributes) {

		if (notEmptyStr(elementName) && attributes != null) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(TAG_START).append(elementName);

			for (String key : attributes.keySet()) {
				String value = attributes.get(key);

				if (value == null) {
					value = "";
				}
				buffer.append(" ").append(key).append("=").append("\"").append(
						value).append("\"");
			}
			return buffer.append(TAG_END).toString();
		}
		return null;
	}

	public static String getElementTagWithAttributes(String elementName,
			Map<String, String> attributes) {

		if (notEmptyStr(elementName) && attributes != null) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(TAG_START).append(elementName);

			for (String key : attributes.keySet()) {
				buffer.append(" ").append(key).append("=").append("\"").append(
						attributes.get(key)).append("\"");
			}
			return buffer.append("/").append(TAG_END).toString();
		}
		return null;
	}

	public static String getElementTag(String elementName) {

		if (notEmptyStr(elementName)) {
			return (new StringBuffer()).append(getElementStartTag(elementName))
					.append(getElementTailTag(elementName)).toString();
		}
		return null;
	}

	public static void writeFile(String directoryPath, String fileName,
			String content) throws IOException {

		if (notEmptyStr(directoryPath) && notEmptyStr(fileName)) {

			FileOutputStream fos = null;

			try {
				File directory = new File(directoryPath);

				if (!directory.exists()) {
					directory.mkdirs();
				}
				File file = new File(directoryPath + File.separator + fileName);
				fos = new FileOutputStream(file);
				byte[] contentBytes = content.getBytes("UTF-8");
				fos.write(XML_HEADER.getBytes("UTF-8"));
				fos.write(contentBytes);

			} finally {
				if (fos != null) {
					fos.close();
				}
			}
		}
	}

	public static boolean notEmptyStr(String value) {

		if (value != null && !"".equals(value)) {
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		try {
			XMLHelper.writeFile("c:\\path", "test.xml", "bcd");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
